$(document).ready(function () {
	href= '/place/save/';
	uploadHref='';
	var id = parseInt($('#id').text());
	if(id ==0){
		$('.imageUploadMain').slideUp();
		$('.uploadImage').slideUp();
	}
	var uploadMain = $('.imageUploadMain').upload({
	    name: 'file',
	    method: 'post',
	    enctype: 'multipart/form-data',
	    action: '/album/upload/main/1/item/place/id/' + $('#id').text(),
	    onComplete: function(data) {
			
			$('.main').html('<img src='+data+'>');
	    }
	});
	
	var upload = $('.uploadImage').upload({
	    name: 'file',
	    method: 'post',
	    enctype: 'multipart/form-data',
	    action: '/album/upload/item/place/id/' + $('#id').text(),
	    onComplete: function(data) {
		 $.ajax({
			   url: '/place/getgallery/id/' + parseInt($('#id').text()),
			   success: function(msg){
			 	
					$('.imagesList').html(msg);
			   }
			});
	    }
	});
	
	function updateImages(){
		 $.ajax({
			   url: '/place/getgallery/id/' + parseInt($('#id').text()),
			   success: function(msg){
					$('.imagesList').html(msg);
			   }
			});
	}
		$(".text").focus(function(){
			if(parseInt($('#id').text()) == 0){
				url  = href;
			} else {
				url = '/place/save/id/' + $('#id').text();
			}
			 $.ajax({
				   type: "POST",
				   url: url,
				   data: $('#placeForm').serialize(),
				   success: function(msg){
					 $('#placeForm').attr('action','/place/add/id/' + $('#id').text());
					 $('#id').text(parseInt(msg));
					 id = parseInt(msg);
				     url = href + 'id/' + parseInt($('#id').text()) + '/';
				     $('.imageUploadMain').slideDown();
				     $('.uploadImage').slideDown();
				     upload.action('/album/upload/item/place/id/' + $('#id').text());
				     uploadMain.action('/album/upload/main/1/item/place/id/' + $('#id').text());
				   }
				});
		});
			
			
			$('.parent').change(function(){
				 $.ajax({
					   type: "POST",
					   data: 'name='+ $(this).attr('name') ,
					   url: '/place/getcategory/id/' + $(this).val(),
					   success: function(msg){
					   		elem = msg; 
					   		$('#'+$(elem).attr('name')).parent().html(elem);
					   }
				});
			});
	
			$('.deleteImage').live('click', function(){
				 $.ajax({
					   type: "POST",
					   data: 'src='+ $(this).attr('href') ,
					   url: '/album/delete/item/place/id/' + id,
					   success: function(msg){
						 $.ajax({
							   url: '/place/getgallery/id/' + parseInt($('#id').text()),
							   success: function(msg){
									$('.imagesList').html(msg);
							   }
							});
					   }
				});
					return false;
			});
	
	i=1;
	$('.plus').live('click',function(){
		i++;
		 $('.category').children('.left').clone(true).appendTo('#nextCategory')
		 	.children('select').attr('name','parent_category'+i).attr('id','parent_category'+i);
		 $('.category').children('.right').clone(true).appendTo('#nextCategory')
		 	.children('select').attr('name','child_category'+i).attr('id','child_category'+i);
		return false;
	});

});
